package cn.mldn.microboot.advice;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * description:
 * Created by gy on 2017/8/14.
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    public static final String DEFAULT_ERROR_VIEW = "error";

//    @ExceptionHandler(Exception.class)
//    public ModelAndView defaultErrorHandler(HttpServletRequest request,Exception e){
//        return new ModelAndView(DEFAULT_ERROR_VIEW).addObject("exception",e).addObject("url",request.getRequestURL());
//    }

    @ExceptionHandler(Exception.class)
    public Object defaultErrorHandler(HttpServletRequest request,Exception e){
        return new ErrorInfo(100,e.toString(),request.getRequestURL().toString());
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class ErrorInfo{
    private Integer code;
    private String message;
    private String url;
}
